package com.neusoft.base;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;


public abstract class BaseServiceImpl<T> implements BaseService<T> {

    private BaseMapper<T> baseDao;

    public abstract BaseMapper<T> getBaseDao();

    public int insert(T entity) {
        return this.getBaseDao().insert(entity);
    }

    public Integer deleteById(Serializable id) {
        return this.getBaseDao().deleteById(id);
    }

    public Integer deleteByIds(Integer[] ids) {
        for (Integer id : ids) {
            deleteById(id);
        }
        return ids.length;
    }

    public void deleteByMap(Map<String, Object> params) {
        this.getBaseDao().deleteByMap(params);
    }


    public void updateById(T entity) {
        this.getBaseDao().updateById(entity);
    }

    /**
     * 查询所有实体
     *
     * @return
     */
    public List<T> listAll() {
        return this.getBaseDao().selectList(new QueryWrapper<T>());
    }

    /**
     * 根据主键获取一个实体
     *
     * @param id
     * @return
     */
    public T getById(Serializable id) {
        return this.getBaseDao().selectById(id);
    }

    /**
     * 批量新增
     */
    public void insertBatch(List<T> list) {
        list.forEach(this::insert);
    }

    /**
     * 批量删除
     */
    public void updateBatch(List<T> list) {
        list.forEach(this::updateById);
    }

    //判断空
    public boolean isEmpty(String str) {
        return (null == str) || (str.trim().length() <= 0);
    }

    public boolean isEmpty(Character cha) {
        return (null == cha) || cha.equals(' ');
    }

    public boolean isEmpty(Object obj) {
        return (null == obj);
    }

    public boolean isEmpty(Object[] objs) {
        return (null == objs) || (objs.length <= 0);
    }

    public boolean isEmpty(Collection<?> obj) {
        return (null == obj) || obj.isEmpty();
    }

    public boolean isEmpty(Set<?> set) {
        return (null == set) || set.isEmpty();
    }

    public boolean isEmpty(Serializable obj) {
        return null == obj;
    }

    public boolean isEmpty(Map<?, ?> map) {
        return (null == map) || map.isEmpty();
    }
}
